package com.srpingde.hhh;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.SimpleImageLoadingListener;
import com.springde.hhh.R;
import com.srpingde.hhh.application.HhhApp;
import com.srpingde.hhh.application.HHHDataManage;
import com.srpingde.hhh.bean.HHHMeiTu;
import com.srpingde.hhh.bean.HHHMeiTuListBean;
import com.srpingde.hhh.bean.HHHMeiTuListDataBean;
import com.srpingde.hhh.http.AsyncHttpResponseHandler;
import com.srpingde.hhh.jiekou.HHHIml;
import com.srpingde.hhh.jiekou.HHHInterface;

public class HHHMeiTuActivity extends Activity {
	HHHMeiTu weiQuMeiTu;
	RadioGroup meitu_rg;
	ImageView back;
	HHHIml weiQuIml;
	HHHMeiTuListBean gouWuChe;
	GridView gridView;
	Map<String,String> map ;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_wei_qv_mei_tu);
		meitu_rg = (RadioGroup) findViewById(R.id.meitu_rg);
		back = (ImageView) findViewById(R.id.back);
		gridView = (GridView) findViewById(R.id.text1);
		map = new HashMap<String, String>();
		layout();
		getMeiTuData();
		meitu_rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				// TODO Auto-generated method stub
				 RadioButton tempButton = (RadioButton)findViewById(checkedId);
				 getItemData(map.get(tempButton.getText()));
			}
		});
		gridView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				// TODO Auto-generated method stub
				Intent in = new Intent(HHHMeiTuActivity.this,HHHMeiTuXiangQing.class);
				in.putExtra("image", gouWuChe.getRows().get(arg2).getBpic());
				in.putExtra("id", gouWuChe.getRows().get(arg2).getBid());
				in.putExtra("name", gouWuChe.getRows().get(arg2).getDesc());
				startActivity(in);
			}
		});
	}
	public void onClick(View v){
    	finish();
    }
	private void getMeiTuData(){
		final Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.fenlei_select);
    	String url = HHHInterface.httpHeader + "acat.php?act=date&page=1&rows=100&parent.keyword=beauty";
    	weiQuIml = new HHHIml();
    	weiQuIml.meiTuShangXiHttp(HHHMeiTuActivity.this, url, new AsyncHttpResponseHandler() {
					@Override
					public void onSuccess(String content) {
						super.onSuccess(content);
						Log.e("mytag", content + "========唯趣天地========");
						weiQuMeiTu = HHHDataManage.getInstance().getQuMeiTu();
						try {
							JSONObject jsonObject = new JSONObject(content);
							JSONArray array = jsonObject.getJSONArray("rows");
							for(int i = 0;i<array.length();i++){
								JSONObject object = array.getJSONObject(i);
								RadioButton android = (RadioButton) LayoutInflater.from(HHHMeiTuActivity.this).inflate(R.layout.radiobutton, null);
								/*RadioGroup.LayoutParams layoutParams = new LayoutParams(bitmap.getWidth(),(int) (WeiQuApp.getInstance().getDisplayHightAndWightPx()[0] * 0.05));
								layoutParams.setMargins((int) (WeiQuApp.getInstance().getDisplayHightAndWightPx()[0] * 0.02), 
								(int) (WeiQuApp.getInstance().getDisplayHightAndWightPx()[0] * 0.07),
								(int) (WeiQuApp.getInstance().getDisplayHightAndWightPx()[0] * 0.02), 0);*/
								android.setText(object.getString("name"));
								android.setId(Integer.valueOf(object.getString("cid")));
								//android.setLayoutParams(layoutParams);
								meitu_rg.addView(android);
								map.put(object.getString("name"), object.getString("cid"));
							}
							meitu_rg.check(meitu_rg.getChildAt(0).getId());
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						
						/*if(weiQuMeiTu!=null&&weiQuMeiTu.getRows().size()>0){
							getItemData(weiQuMeiTu.getRows().get(0).getCid());
						}
						for(int i = 0;i<weiQuMeiTu.getRows().size();i++){
						}*/
						//meitu_rg.check(meitu_rg.getChildAt(0).getId()); 
					}
		});
    }
	/*private void radioButtonLayout(RadioButton button){
		Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.fenlei_select);
		RadioGroup.LayoutParams layoutParams = (android.widget.RadioGroup.LayoutParams) button.getLayoutParams();
		layoutParams.height = (int) (WeiQuApp.getInstance().getDisplayHightAndWightPx()[0] * 0.05);
		layoutParams.width = bitmap.getWidth();
		layoutParams.setMargins((int) (WeiQuApp.getInstance().getDisplayHightAndWightPx()[0] * 0.02), 
		(int) (WeiQuApp.getInstance().getDisplayHightAndWightPx()[0] * 0.07),
		(int) (WeiQuApp.getInstance().getDisplayHightAndWightPx()[0] * 0.02), 0);
		button.setLayoutParams(layoutParams);
	}*/
	private void getItemData(String id){
		
		Log.e("mytag", id);
		String url = HHHInterface.httpHeader + "api.php?pagesize=100&tab=beautypiclist&act=data&cid="+id;
		weiQuIml.weiQuTuPianList(url, HHHMeiTuActivity.this, new AsyncHttpResponseHandler(){

			@Override
			public void onSuccess(String content) {
				// TODO Auto-generated method stub
				super.onSuccess(content);
				Log.e("mytag", "shuju ====="+content);
				gouWuChe =HHHDataManage.getInstance().getWeiQuMeiTuListBean();
				gridView.setAdapter(new WeiquMeiTuAdapter(gouWuChe.getRows(), HHHMeiTuActivity.this));
			}
		 });
	}
	class WeiquMeiTuAdapter extends BaseAdapter{
    	Context context;
    	ArrayList<HHHMeiTuListDataBean> list;
    	public WeiquMeiTuAdapter(ArrayList<HHHMeiTuListDataBean> list, 
    			Context context) {
    		super();
    		this.list = list;
    		this.context = context;
    	}
    	@Override
    	public int getCount() {
    		// TODO Auto-generated method stub
    		return list.size();
    	}

    	@Override
    	public Object getItem(int position) {
    		// TODO Auto-generated method stub
    		return null;
    	}

    	@Override
    	public long getItemId(int position) {
    		// TODO Auto-generated method stub
    		return 0;
    	}
    	private void itemLayout(ImageView imageView){
    		Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.fenlei_select);
    		int wh = ((int) (HhhApp.getInstance().getDisplayHightAndWightPx()[1]-(int) (HhhApp.getInstance().getDisplayHightAndWightPx()[0] * 0.15)))/2;
    		FrameLayout.LayoutParams layoutParams = (android.widget.FrameLayout.LayoutParams) imageView.getLayoutParams();
    		layoutParams.height = wh;
    		layoutParams.width = (int) (HhhApp.getInstance().getDisplayHightAndWightPx()[1]-bitmap.getWidth()-HhhApp.getInstance().getDisplayHightAndWightPx()[0] * 0.02-HhhApp.getInstance().getDisplayHightAndWightPx()[0] * 0.02);
    		imageView.setLayoutParams(layoutParams);
    	}
    	@Override
    	public View getView(int position, View convertView, ViewGroup parent) {
    		// TODO Auto-generated method stub
    		fenLeiHolder holder = null;
    		if(convertView == null){
    			holder = new fenLeiHolder();
    			convertView = LayoutInflater.from(context).inflate(R.layout.weiqulistbean, null);
    			holder.icon = (ImageView) convertView.findViewById(R.id.fenlei_item_icon);
    			convertView.setTag(holder);
    		}else{
    			holder = (fenLeiHolder) convertView.getTag();
    		}
    		itemLayout(holder.icon);
    		//holder.icon.setBackgroundResource(images[arg0]);
    		final ImageView kvShow = holder.icon;
    		ImageLoader.getInstance().loadImage(HHHIml.httpHeader+list.get(position).getSpic(), new SimpleImageLoadingListener(){
    			@Override
    			public void onLoadingComplete(String imageUri, View view, Bitmap bitmap) {
    				BitmapDrawable bitmapDrawable = new BitmapDrawable(bitmap);
    				//kvShow.setImageBitmap(bitmap);
    				kvShow.setBackgroundDrawable(bitmapDrawable);
    			}
    		});
    		return convertView;
    	}
    	class fenLeiHolder{
    		ImageView icon;
    	}
    }
	private void layout() {
		// 头的高度
		TextView top_title_tv = (TextView) findViewById(R.id.top_title);
		RelativeLayout.LayoutParams titleLayoutParams = (android.widget.RelativeLayout.LayoutParams) top_title_tv.getLayoutParams();
		titleLayoutParams.height = HhhApp.getInstance().title_h();
		top_title_tv.setLayoutParams(titleLayoutParams);
		
		RelativeLayout.LayoutParams backLayoutParams = (android.widget.RelativeLayout.LayoutParams) back.getLayoutParams();
		backLayoutParams.height = HhhApp.getInstance().title_h();
		back.setLayoutParams(backLayoutParams);
	}
}
